package com.niuwa.p2p.action.repay;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.entity.business.LoanClaim;
import com.niuwa.p2p.entity.business.RepayPlan;
import com.niuwa.p2p.entity.channel.LoanClaimAudit;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.business.ILoanClaimAuditService;
import com.niuwa.p2p.service.business.ILoanClaimService;
import com.niuwa.p2p.service.business.RepayPlanService;
import com.niuwa.p2p.service.repay.IRepayService;
import com.niuwa.p2p.vo.repay.RepayVo;

/**
 * 还款管理action
 * 
 * @author houdianbo
 * @Date 2015-04-20
 */
@Controller
public class RepayAction extends JsonAction {
	private static final long serialVersionUID = 7300805505675780010L;
	private static Logger logger = Logger.getLogger(RepayAction.class);
	@Autowired
	private IRepayService repayService;
	@Autowired
	private ILoanClaimAuditService loanClaimAuditService;
	@Autowired
	private RepayPlanService repayPlanService;
	@Autowired
	private ILoanClaimService loanClaimService;
	
	private String projectNo;
	
	private LoanClaim loanClaim;
	
	public LoanClaim getLoanClaim() {
		return loanClaim;
	}

	public void setLoanClaim(LoanClaim loanClaim) {
		this.loanClaim = loanClaim;
	}

	private String remark;

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getProjectNo() {
		return projectNo;
	}

	public void setProjectNo(String projectNo) {
		this.projectNo = projectNo;
	}

	/**
	 * 跳转至p2p还款管理
	 * 
	 * @return
	 */
	@Action(value = "gotoRepayManage", results = { @Result(location = "repayManage.jsp", name = SUCCESS) })
	public String gotoRepayManage() {
		logger.info("gotoRepayManage action execute success");
		return SUCCESS;
	}
	
	/**
	 * 跳转至大学生还款管理
	 * 
	 * @return
	 */
	@Action(value = "gotoAppRepayManage", results = { @Result(location = "appRepayManage.jsp", name = SUCCESS) })
	public String gotoAppRepayManage() {
		logger.info("gotoAppRepayManage action execute success");
		return SUCCESS;
	}

	/**
	 * p2p还款列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "searchRepayList")
	public String searchRepayList() throws Exception {
		setObjectReSult(queryPagination());
		logger.info("searchRepayList action execute success");
		return NONE;
	}
	
	/**
	 * 大学生还款列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "searchAppRepayList")
	public String searchAppRepayList() throws Exception {
		setObjectReSult(queryAppPagination());
		logger.info("searchAppRepayList action execute success");
		return NONE;
	}

	/**
	 * 分页查询
	 * 
	 * @return Pagination<Role>
	 */
	private PageBean<RepayVo> queryPagination() {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		logger.debug(paramMap);
		// 执行查询操作
		PageBean<RepayVo> pagination = repayService
				.selectRepayByParam(paramMap);
		return pagination;
	}
	
	/**
	 * 分页查询
	 * 
	 * @return Pagination<Role>
	 */
	private PageBean<RepayVo> queryAppPagination() {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		logger.debug(paramMap);
		// 执行查询操作
		PageBean<RepayVo> pagination = repayService
				.selectAppRepayByParam(paramMap);
		return pagination;
	}

	public void setRepayService(IRepayService repayService) {
		this.repayService = repayService;
	}
	//添加备注信息 repayOverdueRemark.jsp
    @Action("gotoRepayOverdueRemark")
    public String gotoRepayOverdueRemark() {
        return "repayOverdueRemark";
    } 
    
    /**
	 * 添加回访备注
	 * @throws Exception 
	 */	
	@Action("addRepayOverdueRemark")
	public String addRepayOverdueRemark() throws Exception {
		JsonResult result = new JsonResult();	
		LoanClaimAudit loanClaimAudit = new LoanClaimAudit();
		User loginUser = SessionUtil.getSessionUser();
		loanClaimAudit.setOperator(loginUser.getId());
		loanClaimAudit.setOperateType(DictConstant.LOAN_CLAIM_TYPE.OVERDUE_BACKUP.getCode());
		loanClaimAudit.setProjectNo(projectNo);
		loanClaimAudit.setRemark(remark);
		loanClaimAudit.setCreateTime(new Date());
		repayService.addRepayOverdueRemark(loanClaimAudit);;
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	/**
	 * 跳转到回访历史记录页面
	 * 
	 */	
	@Action("gotoRepayOverdueRemarkHistory")
	public String gotoRepayOverdueRemarkHistory() {
		return "repayOverdueRemarkHistory";
	}
	
	/**
	 * 回访历史记录 LoanClaimAuditServiceImpl
	 */	
	@Action("repayOverdueRemarkHistory")
	public String repayOverdueRemarkHistory() {
		Map<String, Object> paramMap = getParamMap();
		paramMap.put("projectNo", projectNo);
		PageBean<LoanClaimAudit> page =  loanClaimAuditService.getRepayOverdueRemarkHistory(paramMap);
		setObjectReSult(page);
		return NONE;
	}
	/**
	 * 跳转到还款计划页面
	 * 
	 */	
	@Action("gotoRepayPlanListPage")
	public String gotoRepayPlanListPage() {
		LoanClaim localLoanClaim = loanClaimService.selectByPrimaryKey(projectNo);
		this.setLoanClaim(localLoanClaim);
		return "repayPlanListPage";
	}
	/**
	 * 
	 * 获取还款计划列表
	 */	
	@Action("repayPlanList")
	public String getRepayPlanList() {
		Map<String, Object> paramMap = getParamMap();
		paramMap.put("projectNo", projectNo);
		PageBean<RepayPlan> page =  repayPlanService.selectRepayPlanPageListByParams(paramMap);
		setObjectReSult(page);
		return NONE;
	}
	
	
}
